package com.yingxin.yxerp.module.print.service.process;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yingxin.yx.framework.commons.activiti.ApprovalStatus;
import com.yingxin.yx.framework.commons.activiti.dto.AuditDto;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yxerp.module.activiti.api.BizProcessHandler;
import com.yingxin.yxerp.module.activiti.api.ProcessService;
import com.yingxin.yxerp.module.organization.entity.User;
import com.yingxin.yxerp.module.organization.service.IUserService;
import com.yingxin.yxerp.module.print.entity.PrintApply;
import com.yingxin.yxerp.module.print.service.IPrintApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Component
public class PrintApplyProcessHandler implements BizProcessHandler {

    @Autowired
    private ProcessService processService;

    @Autowired
    private IPrintApplyService printApplyService;

    @Autowired
    private IUserService userService;


    /**
     * 启动流程实例
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void startProcessInstance(PrintApply apply) {
        // startProcessInstance
        processService.startProcessInstance(BusinessType.DYSQ, apply.getId(), apply.getApplyNo(), getVariables(apply.getId()));
    }

    @Override
    public Map<String, Object> getVariables(String businessId) {
        PrintApply apply = printApplyService.getById(businessId);
        User creator = userService.getById(apply.getUserId());
        Map<String, Object> variables = new HashMap<>();
        if (StringUtils.isNotBlank(creator.getDeptId())) {
            variables.put("deptId", creator.getDeptId());
        }
        return variables;
    }

    @Override
    public void doCommonBiz(String businessId, AuditDto auditDto, ApprovalStatus approvalStatus) {
        UpdateWrapper<PrintApply> uw = new UpdateWrapper<>();
        uw.lambda().set(PrintApply::getApprovalStatus, approvalStatus.getStatus());
        uw.lambda().eq(PrintApply::getId, businessId);
        printApplyService.update(uw);
    }

    @Override
    public void doTaskApprovedBiz(String businessId, AuditDto auditDto, ApprovalStatus approvalStatus) throws IllegalAccessException {

    }

    @Override
    public void doProcessApprovedBiz(String businessId, AuditDto auditDto, ApprovalStatus approvalStatus) throws IllegalAccessException {

    }

    @Override
    public void doProcessUnapprovedBiz(String businessId, AuditDto auditDto, ApprovalStatus approvalStatus) throws IllegalAccessException {

    }




}